package com.lanlian.cayman.view;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.ReqLazyEnum;
import com.lanlian.commonlib.bean.RespLazyEnum;
import com.lanlian.commonlib.bean.UtilEnumInfo;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.cayman.R;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.uikit.WheelView;
import com.lanlian.utillib.ToastUtils;

import java.util.ArrayList;
import java.util.Map;


public class BankSelectorWindow extends Dialog {

    private TextView mTvSure;
    private TextView mTvCancel;
    private WheelView mBanKWheelView;

    private String mBankId;

    private Context mContext;
    private ArrayList<UtilEnumInfo> mBankList = new ArrayList<>();
    private OnBankChooseListener mOnBankChooseListener;

    public void setOnBankChooseListener(OnBankChooseListener onCityChooseListener) {
        this.mOnBankChooseListener = onCityChooseListener;
    }

    public interface OnBankChooseListener {
        void onBankChoose(UtilEnumInfo bank);
    }

    public BankSelectorWindow(final Context context) {
        super(context, R.style.customDialog);
        this.mContext = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.popup_window_bank_selector);
        ViewGroup.LayoutParams params = getWindow().getAttributes();
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        initView();
        getBankList();
        //取消
        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        //设置按钮监听
        mTvSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getBank() != null) {
                    mOnBankChooseListener.onBankChoose(getBank());
                    dismiss();
                } else {
                    ToastUtils.show(mContext, "请选择开户银行");
                }
            }
        });
        if (!TextUtils.isEmpty(mBankId)) {
            for (UtilEnumInfo utilEnumInfo : mBankList) {
                if (utilEnumInfo.getId().equals(mBankId)) {
                    mBanKWheelView.setCurrentIndex(mBankList.indexOf(utilEnumInfo));
                    break;
                }
            }
        }
    }

    private void initView() {
        mTvSure = findViewById(R.id.tv_ok);
        mTvCancel = findViewById(R.id.tv_cancel);
        mBanKWheelView = findViewById(R.id.bank_wv);
    }

    public void showDialog() {
        Window w = getWindow();
        w.setWindowAnimations(R.style.animFromBottom);
        WindowManager.LayoutParams lp = w.getAttributes();
        lp.gravity = Gravity.BOTTOM;
        onWindowAttributesChanged(lp);
        setCanceledOnTouchOutside(true);
        show();
    }

    private void getBankList() {
        VolleyRequestUtils.getInstance().getLazyEnum(mContext.toString(),
                true, true, new ReqLazyEnum(), new INetWorkCallback() {
                    @Override
                    public void onSuccess(Object response) {
                        RespLazyEnum respUtilEnum = (RespLazyEnum) response;
                        ArrayList<String> banks = new ArrayList<>();
                        for (Map.Entry<String, String> entry : respUtilEnum.getBankType().entrySet()) {
                            UtilEnumInfo utilEnumInfo = new UtilEnumInfo();
                            utilEnumInfo.setId(entry.getKey());
                            utilEnumInfo.setName(entry.getValue());
                            banks.add(utilEnumInfo.getName());
                            mBankList.add(utilEnumInfo);
                        }
                        mBanKWheelView.setEntries(banks);
                        mBanKWheelView.setCurrentIndex(0);
                    }

                    @Override
                    public void onFail(BaseResponse baseResponse) {

                    }
                });
    }

    private UtilEnumInfo getBank() {
        if (mBanKWheelView.getCurrentIndex() >= 0) {
            if (mBanKWheelView == null) {
                return null;
            }
            return mBankList.get(mBanKWheelView.getCurrentIndex());
        }
        return null;
    }

    public void setBankId(String bankId) {
        this.mBankId = bankId;
    }

}
